﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.BLL.AppEngine.IService;
using Empires.CRL.Tools;
using Empires.CRL.DataContract;
using Empires.BLL.ServerProvider;

namespace Empires.BLL.AppEngine.Implement
{
    public class BlogService : IBlogService
    {
        public DefaultDataPack GetArticle(int id)
        {
            var obj = DefaultCacheStrategy.GetEntity().RetrieveObject(String.Format("BlogService_GetArticle_{0}", id));
            if (obj == null)
            {
                Empires.RAL.Access.IAppBlogAccess access = new Empires.RAL.Pump.AppBlogDataPump();
                var article = access.GetArticle(id);
                DefaultCacheStrategy.GetEntity().AddObjectWithAbsoluteTime(
                    String.Format("BlogService_GetArticle_{0}", id),
                    article,
                    36000, 
                    System.Web.Caching.CacheItemPriority.Normal);
                return article;
            }
            return (DefaultDataPack)obj;
        }
        public List<DefaultDataPack> GetArticleReply(int articleId, int page, int takeitem)
        {
            Empires.RAL.Access.IAppBlogAccess access = new Empires.RAL.Pump.AppBlogDataPump();
            return access.GetArticleReply(articleId, page, takeitem);
        }

        public int PostArticle(DateTime pbdate, string author, string title, string mainimg, string shortimg, string content)
        {
            if (RoleCheckProvider.CheckUserStatus(
                System.Web.HttpContext.Current.User.Identity.Name,
                status => { return status >= 2; }))
            {
                Empires.RAL.Access.IAppBlogAccess access = new Empires.RAL.Pump.AppBlogDataPump();
                return access.PostArticle(pbdate, author, title, mainimg, shortimg, content);
            }
            return 0;
        }
        public int PostArticleReply(int articleId, string author, string content)
        {
            Empires.RAL.Access.IAppBlogAccess access = new Empires.RAL.Pump.AppBlogDataPump();
            return access.PostArticleReply(articleId, author, content);
        }
        public int EditArticle(int id, string author, string title, string content)
        {
            if (RoleCheckProvider.CheckUserStatus(
               System.Web.HttpContext.Current.User.Identity.Name,
               status => { return status >= 2; }))
            {
                Empires.RAL.Access.IAppBlogAccess access = new Empires.RAL.Pump.AppBlogDataPump();
                return access.EditArticle(id, author, title, content);
            }
            return 0;
        }

        public int DeleteArticle(int id)
        {
            if (RoleCheckProvider.CheckUserStatus(
               System.Web.HttpContext.Current.User.Identity.Name,
               status => { return status >= 2; }))
            {
                Empires.RAL.Access.IAppBlogAccess access = new Empires.RAL.Pump.AppBlogDataPump();
                DefaultCacheStrategy.GetEntity().RemoveObject(String.Format("BlogService_GetArticle_{0}", id));
                return access.DeleteArticle(id);
            }
            return 0;
        }

        /// <summary>
        /// ID = item.id,
        /// DATE = item.c_dt_date.ToLongDateString(),
        /// AUTHOR = item.c_nvc_author,
        /// HIT = item.c_i_scan,
        /// ABSTRACT = content, 截取前200个
        /// ItemCount = itemcount
        /// </summary>
        /// <param name="page"></param>
        /// <param name="takeitem"></param>
        /// <returns></returns>
        public List<DefaultDataPack> GetBlogs(int page, int takeitem)
        {
            var obj = DefaultCacheStrategy.GetEntity().RetrieveObject(String.Format("BlogService_GetBlogse_{0}_{1}", page, takeitem));
            if (obj == null)
            {
                Empires.RAL.Access.IAppBlogAccess access = new Empires.RAL.Pump.AppBlogDataPump();
                var list = access.GetBlogs(page, takeitem).ToList();
                DefaultCacheStrategy.GetEntity().AddObjectWithAbsoluteTime(
                    String.Format("BlogService_GetBlogse_{0}_{1}", page, takeitem),
                    list,
                    36000,
                    System.Web.Caching.CacheItemPriority.Normal);
                return list;
            }
            return (List<DefaultDataPack>)obj;
        }
 
        /// <summary>
        /// ID = item.id,
        /// DATE = item.c_dt_date.ToLongDateString(),
        /// ABSTRACT = content 截取前200个字符
        /// </summary>
        /// <param name="takeitem"></param>
        /// <returns></returns>
        public List<DefaultDataPack> GetPopularBlogs(int takeitem)
        {
            var obj = DefaultCacheStrategy.GetEntity().RetrieveObject(String.Format("BlogService_GetPopularBlogs_{0}", takeitem));
            if (obj == null)
            {
                Empires.RAL.Access.IAppBlogAccess access = new Empires.RAL.Pump.AppBlogDataPump();
                var list = access.GetPopularBlogs(takeitem).ToList();
                DefaultCacheStrategy.GetEntity().AddObjectWithAbsoluteTime(
                    String.Format("BlogService_GetPopularBlogs_{0}", takeitem),
                    list,
                    36000,
                    System.Web.Caching.CacheItemPriority.Normal);
                return list;
            }
            return (List<DefaultDataPack>)obj;
        }
    }
}
